package dao;

import com.mongodb.WriteResult;
import entity.Product;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.FindOptions;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.mongodb.morphia.query.UpdateResults;
import util.MorphiaUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static util.MorphiaUtil.getDB;

public class ProductDAO {
    //添加
    public void insert(Product product) {
        Datastore ds = getDB();
        ds.save(product);
    }
    //删除
    public int deleteOne(ObjectId id){
        Datastore db=getDB();
        Query<Product> query= db.createQuery(Product.class);
        query.filter("_id",id);
        WriteResult delete = db.delete(query);
        return delete.getN();
    }
    //根据id查询
    public Product findById(ObjectId id){
        Datastore db=getDB();
        //创建查询条件对象
        Query<Product> query= db.createQuery(Product.class);
        //设置查询条件
        query.filter("_id",id);
        Product p=query.get();
        return p;
    }
    //查询全部
    public List<Product> findAll() {
        Datastore ds = getDB();
        Query<Product> query = ds.createQuery(Product.class);
        List<Product> products = query.asList();
        return products;
    }
    //分页查询
    public List<Product> findPage(int skip,int limit) {
        Datastore ds = getDB();
        Query<Product> query = ds.createQuery(Product.class);
        FindOptions findOptions = new FindOptions();
        findOptions.skip(skip).limit(limit);
        // 设置按时间排序
        query.order("-addDate");
        List<Product> products = query.asList(findOptions);
        return products;
    }
    //统计总数
    public long getCount(){
        return getDB().getCount(Product.class);
    }
    //修改
    public int update(Product product) {
        Datastore ds = getDB();
        Query<Product> query = ds.createQuery(Product.class);
        query.filter("_id",product.getId());
        //query.filter("_id",product.getId());
        UpdateOperations<Product> update = ds.createUpdateOperations(Product.class);
        if(product.getName()!=null){
            update.set("name",product.getName());
        }
        if (product.getPrice()!=null){
            update.set("price",product.getPrice());
        }
        if (product.getSum()!= null){
            update.set("sum",product.getSum());
        }
        if (product.getVisitCount()!=null){
            update.set("visitCount",product.getVisitCount());
        }
        if (product.getStatus()==0||product.getStatus()==1){
            update.set("status",product.getStatus());
        }
        if (product.getAddDate()!=null){
            update.set("addDate",product.getAddDate());
        }
        if (product.getInfo()!=null){
            update.set("info",product.getInfo());
        }
        if (product.getImageUrl()!=null){
            update.set("imageUrl",product.getImageUrl());
        }
        UpdateResults u =ds.updateFirst(query,update);
        return u.getUpdatedCount();
    }

    public void updateStatus(String id){
        Datastore ds = getDB();
        ProductDAO productDAO=new ProductDAO();
        Product product=productDAO.findById(new ObjectId(id));
        System.out.println(product);


        if(product.getStatus()==0){
            product.setStatus(1);
        }
        else if(product.getStatus()==1){
            product.setStatus(0);
        }

        ds.save(product);}

    // 按名称模糊分页查询
    public Map<Long, List<Product>> findPageByName(String name, int skip, int limit) {
        Datastore ds = MorphiaUtil.getDB();
        Query<Product> query = ds.createQuery(Product.class);
        // 当name不为空时，添加查询条件
        if (name != null && !name.isEmpty())
            query.field("name").contains(name.trim());
        // 必须是已上架的商品
        query.field("status").equal(1);
        // 库存大于0
        query.field("sum").greaterThan(0);
        FindOptions findOptions = new FindOptions();
        findOptions.skip(skip).limit(limit);
        // 得到数据总条数
        long total = query.count();
        //System.out.println("total: " + total);
        List<Product> products = query.asList(findOptions);
        Map<Long, List<Product>> map = new HashMap<>();
        map.put(total, products);
        return map;
    }
}
